<?php


namespace wechat\lib;

use wechat\lib\core\Auth;
use wechat\lib\core\Base;
use wechat\lib\core\IAuth;
use wechat\lib\core\IRequestMsg;
use wechat\lib\core\IResponseMsg;
use wechat\lib\core\Media;
use wechat\lib\core\Menu;
use wechat\lib\core\RequestMsg;
use wechat\lib\core\ResponseMsg;
use wechat\lib\core\User;

/**
 * Class Wechat
 * @package wechat\lib
 * @auth: Taurus12C
 * @email: 1402410174@qq.com
 * @date: 2019-12-04 10:27:35
 */
class Wechat extends Base implements IAuth,IRequestMsg,IResponseMsg
{
    use Auth,RequestMsg,ResponseMsg;

    /**
     * @desc 微信 服务号|订阅号 appid
     * @var bool|string
     */
    protected $appid;

    /**
     * @desc 微信 服务号|订阅号 appsecret
     * @var bool|string
     */
    protected $appsecret;

    /**
     * @desc 微信 服务号|订阅号 token
     * @var bool|string
     */
    protected $token;

    /**
     * Wechat constructor.
     * @param bool $appid
     * @param bool $appsecret
     * @param bool $token
     */
    public function __construct($appid=false, $appsecret=false, $token=false)
    {
        $this->appid = $appid?$appid:Config::wechat('appid');
        $this->appsecret = $appsecret?$appsecret:Config::wechat('appsecret');
        $this->token = $token?$token:Config::wechat('token');
    }

    public function __get($name)
    {
        // TODO: Implement __get() method.
        $obj = strtolower($name);
        switch ($obj){
            case 'user':
                return User::getInstance($this->appid,$this->appsecret);
            break;
            case 'menu':
                return Menu::getInstance($this->appid,$this->appsecret);
                break;
            case 'media':
                return Media::getInstance($this->appid,$this->appsecret);
                break;
            default:
                try {
                    throw new \Exception('不存在的'.$name.'类对象');
                }catch (\Exception $e){
                    echo $e->getFile().'::Line'.$e->getLine().'<br>'.$e->getMessage();
                }

            break;
        }

    }

    /**
     * @desc 引导用户前往微信授权页面
     * @param $redirect_uri 授权后重定向地址
     * @param $scope 应用授权作用域
     * @param string $state 授权回调参数
     */
    public function webAuth(string $redirect_uri, string $scope, string $state='state'):void
    {
        $this->getCode($this->appid,$redirect_uri,$scope,$state);
    }

    /**
     * @desc 获取用户基本信息
     * @return string|mixed
     */
    public function getUserInfo(string $redirect_uri='')
    {

        if (isset($_GET['code'])){
            $code = $_GET['code'];
            $str = $this->userInfo($this->appid,$this->appsecret,$code);
            $res = $this->checkError($str);
            if ($res){
                return $str;
            }
            return false;
        }

        $redirect_url = $redirect_uri?$redirect_uri:'http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
        $this->webAuth($redirect_url,'snsapi_userinfo');

    }

    /**
     * @desc 获取用户openid
     * @return mixed
     */
    public function getOpenId(string $redirect_uri='')
    {

        if (isset($_GET['code'])){
            $code = $_GET['code'];
            $str = $this->openId($this->appid,$this->appsecret,$code);
            $res = $this->checkError($str);
            if ($res){
                return $str;
            }
            return false;
        }

        $redirect_url = $redirect_uri?$redirect_uri:'http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
        $this->webAuth($redirect_url,'snsapi_base');
    }

    /**
     * @desc 接收消息
     * @return array
     */
    public function getMsg(): array
    {
        return $this->Msg();
    }

    /**
     * @desc 发送文本
     * @param string $toUserName
     * @param string $fromUserName
     * @param string $content
     */
    public function sendText(string $toUserName, string $fromUserName, string $content): void
    {
        $this->text($toUserName,$fromUserName,$content);
    }

    /**
     * @desc 发送图片
     * @param string $toUserName
     * @param string $fromUserName
     * @param string $mediaId
     */
    public function sendImg(string $toUserName, string $fromUserName, string $mediaId): void
    {
        $this->img($toUserName,$fromUserName,$mediaId);
    }

    /**
     * @desc 发送语音
     * @param string $toUserName
     * @param string $fromUserName
     * @param string $mediaId
     */
    public function sendVoice(string $toUserName, string $fromUserName, string $mediaId): void
    {
        $this->voice($toUserName,$fromUserName,$mediaId);
    }

    /**
     * @desc 发送音乐
     * @param string $toUserName
     * @param string $fromUserName
     * @param array $content
     */
    public function sendMusic(string $toUserName, string $fromUserName, array $content): void
    {
        $this->music($toUserName,$fromUserName,$content);
    }

    /**
     * @desc 发送视频
     * @param string $toUserName
     * @param string $fromUserName
     * @param array $content
     */
    public function sendVideo(string $toUserName, string $fromUserName, array $content): void
    {
        $this->video($toUserName,$fromUserName,$content);
    }

    /**
     * @desc 发送图文
     * @param string $toUserName
     * @param string $fromUserName
     * @param array $articles
     */
    public function sendNews(string $toUserName, string $fromUserName, array $articles): void
    {
        $this->news($toUserName,$fromUserName,$articles);
    }

}